package io.github.wppli.infrastructure.persistent.repository;

import io.github.wppli.domain.user.model.entity.FollowUserEntity;
import io.github.wppli.domain.user.repository.IAdjustAccountRepository;
import io.github.wppli.infrastructure.persistent.dao.IUserDao;
import io.github.wppli.infrastructure.persistent.dao.IUserFollowsDao;
import io.github.wppli.infrastructure.persistent.po.User;
import io.github.wppli.infrastructure.persistent.po.UserFollows;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author li--jiaqiang 2025−03−03
 */
@AllArgsConstructor
@Repository
public class AdjustAccountRepository implements IAdjustAccountRepository {

    private final IUserDao userDao;
    private final IUserFollowsDao userFollowsDao;

    /**
     * @param userId 用户id
     * @param targetUserId 目标用户id
     * @param followed true: 关注， false: 取关
     */
    @Override
    @Transactional
    public void adjustFollowCount(Long userId, Long targetUserId, boolean followed) {

        UserFollows userFollows = userFollowsDao.selectUserFollowStatus(userId, targetUserId);

        if (followed) {
            // 增加关注
            userDao.increaseFollowCount(userId);
            // 增加粉丝
            userDao.increaseFollowerCount(targetUserId);
            // 更新关注状态
            if (userFollows == null) {
                userFollowsDao.insert(new UserFollows(null, userId, targetUserId, new Date(), new Date(), 1));
            } else {
                userFollowsDao.updateUserFollowStatusNormal(userId, targetUserId);
            }
        } else {
            userDao.decreaseFollowCount(userId);
            userDao.decreaseFollowerCount(targetUserId);
            userFollowsDao.updateUserFollowStatusDelete(userId, targetUserId);
        }
    }

    @Override
    public List<FollowUserEntity> getFolloweeList(Long userId) {
        List<String> followeeList = userFollowsDao.queryUserFolloweeList(userId);
        if(null == followeeList || followeeList.isEmpty()) {
            return Collections.emptyList();
        }
        // 查询用户信息
        List<User> users = userDao.selectBatchIds(followeeList);
        if(null == users || users.isEmpty()) {
            return Collections.emptyList();
        }
        List<FollowUserEntity> result = new LinkedList<>();
        for (User user : users) {
            FollowUserEntity followUserEntity = FollowUserEntity.builder()
                        .id(user.getId())
                        .username(user.getUsername())
                        .followCount(user.getFollowCount())
                        .followerCount(user.getFollowerCount())
                        .favoriteCount(user.getFavoriteCount())
                        .avatarUrl(user.getAvatarUrl())
                        .build();
            result.add(followUserEntity);
        }
        return result;
    }
}